package com.trainoo.novel.crawler;

import com.trainoo.novel.constant.NovelConstant;
import com.trainoo.novel.model.Book;
import com.trainoo.novel.model.Chapter;
import com.trainoo.novel.util.UrlDownLoader;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
 * Created by Administrator on 2018/8/25.
 */
@Component
public class SoduNovelCrawler extends NovelCrawlerAdapter{

    private static Logger logger = LoggerFactory.getLogger(BqgNovelCrawler.class);

    @Value("${base.url.sodu}")
    private String BASE_URL;

    @Value("${search.url.sodu}")
    private String SEARCH_URL;

    @Override
    public Book crawlBookInfo(String url) {
        // 返回结果集
        Book book = new Book();
        // 解析网页
        Document doc = UrlDownLoader.download(BASE_URL + "/book/" + url + ".html");
        // 小说名、作者、简介
        book.setBookName(doc.title());
        book.setMainInfo(doc.title());
        // 保存章节信息
        Elements tr = doc.getElementsByClass("list").get(0).getElementsByTag("tr");
        List<Chapter> chapList = new ArrayList<>();
        for (int i = 1; i < tr.size(); i++) {
            String chapName = tr.get(i).getElementsByTag("a").get(0).html();
            String chapUrl = tr.get(i).getElementsByTag("a").get(0).attr("href");
            Chapter chap = new Chapter(chapName, URLEncoder.encode(BASE_URL + chapUrl));
            chapList.add(chap);
        }
        book.setChapters(chapList);
        logger.info("正在加载图书[{}]详情", doc.title());
        return book;
    }

    @Override
    public Chapter readChapter(String url) {
        return null;
    }

    @Override
    public List<Chapter> hotBookList() {
        Document doc = UrlDownLoader.download(BASE_URL);
        Elements aList = doc.getElementsByTag("a");
        List<Chapter> hotList = new ArrayList<>();
        Set<String> urls = new HashSet<>();
        for (int i = 0; i < aList.size(); i++) {
            String html = aList.get(i).html();
            String href = aList.get(i).attr("href");
            if (href.endsWith(".html") && href.contains("/book/")){
                // 使用set去重
                if(urls.add(href)){
                    href = href.replace(".html", "");
                    hotList.add(new Chapter(html, href));
                }
            }
        }
        return hotList;
    }

    @Override
    public List<Book> searchBook(Map<String, String> contentMap) {
        String url = SEARCH_URL + formatSearchUrl(contentMap.get(NovelConstant.KEY_WORD));
        Document document = UrlDownLoader.download(url);
        Elements trs = document.getElementsByTag("tbody").get(3).getElementsByTag("tr");
        List<Book> resultList = new ArrayList<>();
        for (int i = 1; i < trs.size(); i ++){
            Elements tds = trs.get(i).getElementsByTag("td");
            String bookUrl = tds.get(1).getElementsByTag("a").first().attr("href").replace(".html", "/");
            String bookName = tds.get(1).getElementsByTag("a").first().text();
            String newUrl = tds.get(2).getElementsByTag("a").first().attr("href").replace(".html", "/");
            String newName = tds.get(2).getElementsByTag("a").first().text();

            Book book = new Book();
            book.setBookUrl(bookUrl);
            book.setBookName(bookName);
            book.setNewUrl(newUrl);
            book.setNewName(newName);
            resultList.add(book);
        }
        return resultList;
    }

    private String formatSearchUrl(String keyword){
        try {
            return "?keyword=" + URLEncoder.encode(keyword, "utf-8");
        } catch (UnsupportedEncodingException e) {
            return "?keyword=" + keyword;
        }
    }
}
